<!doctype html>
<html>
  <head>
    <title>HTML Imports Test</title>
    <script src="../../tools/test/htmltest.js"></script>
    <script src="../../node_modules/chai/chai.js"></script>
    <script src="../../html-imports.js"></script>
    <link rel="import" href="imports/import-1.html">
  </head>
  <body>
    <script>
      var timeout = setTimeout(function() {
        chai.assert.fail(null, null, 'timed out');
      }, 1000);
      addEventListener('HTMLImportsLoaded', function() {
        clearTimeout(timeout);
        //console.dir(Object.keys(HTMLImports.cache));
        chai.assert.equal(4, Object.keys(HTMLImports.importer.cache).length,
          'must cache exactly four resources');
        chai.assert.equal(4, Object.keys(HTMLImports.importer.documents).length,
          'must cache exactly four documents');

        Object.keys(HTMLImports.importer.documents).forEach(function(key) {
          var doc = HTMLImports.importer.documents[key];
          var links = doc.querySelectorAll('link[rel=import]');
          Array.prototype.forEach.call(links, function(link) {
            var href = link.getAttribute('href');
            if (href.indexOf('404') <= 0) {
              chai.assert.isDefined(link.import.content, 'import should have content');
            }
          });
        })
        done();
      });
    </script>
  </body>
</html>
